package com.lee.security.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.lee.security.dao.SysUsersRepository;
import com.lee.security.entity.SysRole;
import com.lee.security.entity.SysUser;

@Repository
public class SysUsersRepositoryImpl implements SysUsersRepository { 
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
      
	@Override
	public SysUser getByUsername(String username) {
		String sql = "select user_id userId, username, password, enabled from s_users where username = ? ";
		SysUser sysUser = jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper<SysUser>(SysUser.class));
		return sysUser;
	}
	
	@Override
	public List<SysRole> getSysRolesByUsername(String username) {
		String sql = "select r.role_id roleId, r.role_code roleCode, r.role_name roleName from s_roles r, s_users u where r.user_id = u.user_id and u.username = ? ";
		List<SysRole> srl = jdbcTemplate.query(sql,new Object[] { username }, new BeanPropertyRowMapper<SysRole>(SysRole.class));
    	return srl;
	}  
}  